const puppeteer = require("puppeteer");
const fs = require("fs");
const readline = require("readline");

async function init() {
  async function getDataValue(name, el) {
    const jobNameEl = await el.$("." + name);
    const elInnerText = await jobNameEl.getProperty("innerText");
    const value = await elInnerText.jsonValue();
    return value;
  }
  async function getResult() {
    let code = 1;
    const result = [];
    // 1. 创建浏览器
    const browser = await puppeteer.launch({ headless: false });
    // 2. 创建页面
    const page = await browser.newPage();
    // 3. 设置页面视口大小
    await page.setViewport({ width: 1000, height: 1080 });

    while (code <= pageCode) {
      // 4. 打开网页
      await page.goto(
        `https://www.zhipin.com/web/geek/job?query=${keys}&city=101010100&page=${code}`
      );
      // 5. 等待页面打开
      await page.waitForSelector(".job-list-box");
      //                          job-card-wrapper
      const els = await page.$$(".job-card-wrapper");

      for await (const el of els) {
        const jobName = await getDataValue("job-name", el);
        const jobArea = await getDataValue("job-area", el);
        const salary = await getDataValue("salary", el);
        const tagList = await getDataValue("tag-list", el);
        const companyName = await getDataValue("company-name", el);
        const companyTagList = await getDataValue("company-tag-list", el);

        const data = {
          jobName,
          jobArea,
          salary,
          tagList,
          companyName,
          companyTagList,
        };
        result.push(data);
      }
      console.log(`第${code}页爬取完毕`);
      code++;
    }
    await browser.close();
    console.log("爬取完毕", `总数据${result.length}`);
    return result;
  }
  async function writeFile(writeUrl, data) {
    const dir = writeUrl.split("/");

    dir.forEach((item, index) => {
      if (index !== dir.length - 1) {
        const path = dir.slice(0, index + 1).join("/");
        if (!fs.existsSync(path)) {
          // 创建目录path
          fs.mkdirSync(path);
        }
      }
    });
    // 写入文件 writeUrl
    console.log("写入文件", writeUrl);
    fs.writeFileSync(writeUrl, JSON.stringify(data));
    console.log("写入完成", writeUrl);
  }
  function getProcessInput(question) {
    return new Promise((resolve, reject) => {
      // 创建接口实例
      let r1 = readline.createInterface({
        input: process.stdin,
        output: process.stdout,
      });

      r1.question(question, (answer) => {
        resolve(answer.trim());
        r1.close();
      });
    });
  }
  console.log("按回车继续下一个问题！！！！");
  const city =
    (await getProcessInput("请输入爬虫的城市(默认北京)：")) || "北京";
  const keys =
    (await getProcessInput("请输入爬虫的职位（默认前端）：")) || "前端";
  const pageCode =
    (await getProcessInput("请输入的页数(每页30条)（默认10页）: ")) || 10;

  console.log(`爬虫开始，爬取${city}的${keys}职位，共${pageCode}页`);

  const data = await getResult();
  const date = new Date().toLocaleDateString();
  const writeUrl = `./data/${date}/${city}-${keys}-${data.length}.json`;
  writeFile(writeUrl, data);
}

init();
